package com.course.android.activity.ac;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.baselib.android.utils.ToastTool;
import com.baselib.android.utils.img.ImageLoader;
import com.course.android.R;
import com.course.android.activity.ActivityIntent;
import com.course.android.activity.base.BaseActivity;
import com.course.android.activity.base.BaseNetActivity;
import com.course.config.UrlConfig;
import com.course.controller.UploadController;
import com.course.controller.permission.PermissionController;
import com.course.controller.permission.PermissionReqCode;
import com.course.model.HttpConfigBean;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.List;

public class UploadImageAcivity extends BaseNetActivity {
    private int RQUEST_IMG = 100;

    ImageView iv, netIv;
    View uploadTv;

    @Override
    protected void initView() {
        uploadTv = findViewById(R.id.uploadTv);
        netIv = findViewById(R.id.netIv);

        findViewById(R.id.selectTv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog permissionDialog = PermissionController.getPersissionDialog(UploadImageAcivity.this,
                        PermissionReqCode.REQ_SELECT_ALBUM);
                if (permissionDialog == null) {
                    selectSysImage();
                } else {
                    permissionDialog.show();
                }
            }
        });
        iv = findViewById(R.id.iv);


    }

    @Override
    protected void initData() {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_upload_imge_demo;
    }

    @Override
    protected void onActivityResultCallBack(Integer reqCode, Intent data) {

    }

    @Override
    public void granted(String[] permissions, int reqCode) {
        if (reqCode == PermissionReqCode.REQ_SELECT_ALBUM) {
            selectSysImage();
        }
    }

    void selectSysImage() {
        String prider = getApplication().getPackageName() + ".provider";
        Matisse.from(UploadImageAcivity.this)
                .choose(MimeType.ofAll()) // 选择 mime 的类型
                .countable(true)
                .capture(true)
                .captureStrategy(new CaptureStrategy(false, prider))
                .maxSelectable(9) // 图片选择的最多数量
//                .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.normal_30))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
//                .thumbnailScale(1f) // 缩略图的比例
                .imageEngine(new GlideEngine()) // 使用的图片加载引擎
                .forResult(RQUEST_IMG); // 设置作为标记的请求码
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != UploadImageAcivity.RESULT_OK) {
            return;
        }
        if (requestCode == RQUEST_IMG) {
            List<String> paths = Matisse.obtainPathResult(data);
            if (paths == null || paths.size() == 0) {
                return;
            }
            String path = paths.get(0);
            Log.e("-----", path);
            ImageLoader.getInstance(this).displayLocalImage(path, iv);
            uploadTv.setVisibility(View.VISIBLE);
            UploadController.getInstance().uploadImg(path, this);
        }
    }

    @Override
    public <String> void onData(HttpConfigBean flag, String o) {
        ToastTool.show(this, UrlConfig.getImageUrl((java.lang.String) o));

        ImageLoader.getInstance(this).displayImage(UrlConfig.getImageUrl((java.lang.String) o), netIv);
    }

    @Override
    public <T> void onPageData(HttpConfigBean flag, int pageNum, int pageSize, int total, List<T> array) {

    }
}
